#!/usr/bin/env ruby

# This script populates the library with shims within myExperiment workflows

env = "development"

unless ARGV[0].nil? or ARGV[0] == ''
  env = ARGV[0]
end

RAILS_ENV = env

# Load up the Rails app
require File.join(File.dirname(__FILE__), '..', 'config', 'environment')

class TestData
  
  TAGS = ['science','biology','protein','dna','rna','gene','nucleotide','microbiology','sequence']
  WORDS = ['shim','good','great','input','output','strings','data','biology','bio','science','list',
           'depth','information','server','web','service']
  SCRIPT_TYPES = ['BeanShell','Other','Workflow']
          
  SERVICE_TYPES = ['REST Service','SOAP Service']
  
  def initialize
    @@users = User.all
  end
  
  def make_description
    desc = ""
    (rand(30)+5).times do |x|
      desc << WORDS[rand(WORDS.length)] + " "
    end
    return desc.chop!.capitalize! << "."
  end
  
    
  def add_data
    print "Deleting existing data\n"
    Shim.delete_all
    ShimInput.delete_all
    ShimOutput.delete_all
    Annotation.delete_all
    ShimBlob.delete_all
    Service.delete_all
    ServiceLink.delete_all
    Category.delete_all
    
    
    #Creating categories
    print "\nCreating categories\n"
    
    8.times do |x|
      Category.create(:name => "Category#{x+1}")
    end
    8.times do |x|
      Category.create(:name => "Category#{x+9}", :parent_id => rand(8)+1)
    end
    8.times do |x|
      Category.create(:name => "Category#{x+17}", :parent_id => rand(16)+1)
    end
    
    #Creating categories
    print "\nCreating services\n"
    
    @@services = Array.new
    
    20.times do |x|
      @@services << Service.create(:name => "Service#{x}", :endpoint_url => "http://www.service#{x}.org", :biocat_link => (x+180).to_s)
    end


    #Creating shims
    print "\nCreating shims\n"
    
    15.times do |s|
      #Create shim
      print "\nCreating shim\n"
        #Find a random user
        user = @@users[rand(@@users.length)]
        
      if (rand(2) == 1)
        chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789 \n"
        #Make a random file
        fileContents = ''
        rand(1000).times { |i| fileContents << chars[rand(chars.length)] }  
        blob = ShimBlob.create(:data => fileContents)          
        unless blob.errors.empty?
          print "Blob Creation Error: "
          print blob.errors.full_messages
          print "\n" 
        end
        @shim = Shim.create(:name => "shim#{s}", :shim_type => SCRIPT_TYPES[rand(3)], :user_id => user.id,
                           :shim_blob_id => blob.id,:content_type => "text/plain", :filename => "file#{s}.txt")
      else
        url_chars = "abcdefghjkmnpqrstuvwxyz0123456789"
        url = ''
        (rand(15)+4).times { |i| url << url_chars[rand(url_chars.length)] }  
        serv = Service.create(:name => "ShimService#{s}", :endpoint_url => "http://www.#{url}.org:#{s}", :biocat_link => s.to_s)
        unless serv.errors.empty?
          print "Service Creation Error: "
          print serv.errors.full_messages
          print "\n" 
        end
        @shim = Shim.create(:name => "shim#{s}", :shim_type => SERVICE_TYPES[rand(2)], :user_id => user.id, :service_id => serv.id)
      end
      
      unless @shim.errors.empty?
        print "Error: "
        print @shim.errors.full_messages
        print "\n" 
      end
    
      #Shim tags
      print "--Adding tags\n"
      rand(6).times do |x|
        #Find a random user
        ann_user = @@users[rand(@@users.length)]
        
        Annotation.create(:value => TAGS[rand(TAGS.length)], :attribute_name => "tag",
                          :annotatable_type => "Shim", :annotatable_id => @shim.id,
                          :source_type => "User", :source_id => ann_user.id)
      end
      
      #Shim descriptions
      print "--Adding descriptions\n"
      rand(4).times do |x|
        #Find a random user
        ann_user = @@users[rand(@@users.length)]
        
        Annotation.create(:value => make_description, :attribute_name => "description",
                          :annotatable_type => "Shim", :annotatable_id => @shim.id,
                          :source_type => "User", :source_id => ann_user.id)
      end
      
      #Shim categories
      print "--Adding categories\n"
      (rand(3)+1).times do |x|
        #Find a random user
        ann_user = @@users[rand(@@users.length)]
        
        Annotation.create(:value => Category.list[rand(Category.list.length)].id.to_s , :attribute_name => "category",
                          :annotatable_type => "Shim", :annotatable_id => @shim.id,
                          :source_type => "User", :source_id => ann_user.id)
      end
      
      #Shim example workflows
      print "--Adding example workflows\n"
      (rand(3)+1).times do |x|
        #Find a random user
        ann_user = @@users[rand(@@users.length)]
        
        Annotation.create(:value => "http://www.myexperiment.org/workflows/#{(rand(1000)+1)}" , :attribute_name => "example_workflow",
                          :annotatable_type => "Shim", :annotatable_id => @shim.id,
                          :source_type => "User", :source_id => ann_user.id)
      end
      
      #Add shim inputs
      print "--Adding inputs\n"
      (rand(2)+1).times do |i|
        print "--Input#{i}\n"
        shiminput = ShimInput.create(:name => "in#{i}", :shim_id => @shim.id)
        #Shim input tags
        print "----Adding tags\n"
        rand(6).times do |x|
          #Find a random user
          ann_user = @@users[rand(@@users.length)]
          
          Annotation.create(:value => TAGS[rand(TAGS.length)], :attribute_name => "tag",
                            :annotatable_type => "ShimInput", :annotatable_id => shiminput.id,
                            :source_type => "User", :source_id => ann_user.id)
        end
        
        #Shim input descriptions
        print "----Adding descriptions\n"
        rand(4).times do |x|
          #Find a random user
          ann_user = @@users[rand(@@users.length)]
          
          Annotation.create(:value => make_description, :attribute_name => "description",
                            :annotatable_type => "ShimInput", :annotatable_id => shiminput.id,
                            :source_type => "User", :source_id => ann_user.id)
        end
        
        #Shim input source services
        print "----Adding source services\n"
        (rand(2)+1).times do |x|
          #Find a random user
          ann_user = @@users[rand(@@users.length)]
          
          #Find a random service
          service = @@services[rand(@@services.length)]
          
          ServiceLink.create(:port_type => "ShimInput", :port_id => shiminput.id,
                            :service_id => service.id, :operation_link => "a link",
                            :source_id => ann_user.id)
        end
      end#end shim inputs
      
      #Add shim outputs
      print "--Adding outputs\n"
      (rand(2)+1).times do |o|
        print "--Output#{o}\n"
        shimoutput = ShimOutput.create(:name => "out#{o}", :shim_id => @shim.id)
        #Shim output tags
        print "----Adding tags\n"
        rand(6).times do |x|
          #Find a random user
          ann_user = @@users[rand(@@users.length)]
          
          Annotation.create(:value => TAGS[rand(TAGS.length)], :attribute_name => "tag",
                            :annotatable_type => "ShimOutput", :annotatable_id => shimoutput.id,
                            :source_type => "User", :source_id => ann_user.id)
        end
        
        #Shim output descriptions
        print "----Adding descriptions\n"
        rand(4).times do |x|
          #Find a random user
          ann_user = @@users[rand(@@users.length)]
          
          Annotation.create(:value => make_description, :attribute_name => "description",
                            :annotatable_type => "ShimOutput", :annotatable_id => shimoutput.id,
                            :source_type => "User", :source_id => ann_user.id)
        end
        
        #Shim output destination services
        print "----Adding destination services\n"
        (rand(2)+1).times do |x|
          #Find a random user
          ann_user = @@users[rand(@@users.length)]
          
          #Find a random service
          service = @@services[rand(@@services.length)]
          
          ServiceLink.create(:port_type => "ShimOutput", :port_id => shimoutput.id,
                            :service_id => service.id, :operation_link => "a link",
                            :source_id => ann_user.id)
        end
      end#end shim outputs
    end #create shim end 
    
    print "\n\n DONE \n"
  end  
end

data = TestData.new
data.add_data